package servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import manager.AdministradorMesa;
import manager.AdministradorPedido;
import manager.AdministradorPlato;
import NegocioVO.ComandaVO;
import NegocioVO.EmpleadoVO;
import NegocioVO.MesaVO;
import NegocioVO.PlatoVO;

/**
 * Servlet implementation class ControladorPedido
 */
public class ControladorPedido extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        String jspPage = null;
       
       
        if ((action == null) || (action.length() < 1))
        {
            action = "default";
        }
        
        if ("default".equals(action))
        {
        	String cookieName = "userLocal";
        	Cookie cookies[] = request.getCookies();
        	Cookie myCookie = null;
        	String aux = null;
        	if (cookies != null) {
        		for (int i = 0; i < cookies.length; i++) {
        			if (cookies[i].getName().equals(cookieName)) {
        				myCookie = cookies[i];
        				break;
        			}
        		}
        	}
        	String LocalId;
        	System.out.println("la coockie es la numero: " + myCookie.getValue());
        	LocalId = myCookie.getValue();
        	
        	List<MesaVO> mesasOc = new ArrayList<MesaVO>();
        	mesasOc = AdministradorMesa.getInstancia().getMesasOcupadas(LocalId);
			request.setAttribute("mesasOc", mesasOc);
            
            List<EmpleadoVO> mozos = new ArrayList<EmpleadoVO>();
            mozos = AdministradorMesa.getInstancia().getMozosLocal(LocalId);
			request.setAttribute("mozos", mozos);
			
			List<ComandaVO> platos = new ArrayList<ComandaVO>();
        	platos = AdministradorPlato.getInstancia().getPlatos(LocalId);
			request.setAttribute("platos", platos);
            
			jspPage = "/jsp/AbrirPedido.jsp";
        }
        
        else if ("abrirPedido".equals(action)){
            
        	//traer el idmesa, idmozo y list de comandas
        	
        	String cookieName = "userLocal";
        	Cookie cookies[] = request.getCookies();
        	Cookie myCookie = null;
        	String aux = null;
        	if (cookies != null) {
        		for (int i = 0; i < cookies.length; i++) {
        			if (cookies[i].getName().equals(cookieName)) {
        				myCookie = cookies[i];
        				break;
        			}
        		}
        	}
        	String LocalId;
        	System.out.println("la coockie es la numero: " + myCookie.getValue());
        	LocalId = myCookie.getValue();
        	
        	
        	int idMesa = Integer.valueOf(request.getParameter("mesas"));
        	int nroMozo = Integer.valueOf(request.getParameter("mozos"));
        	

        	String[] comandas = request.getParameterValues("checkbox");
        	String[] cantidades = request.getParameterValues("cantElegida");
        	

        	
        	AdministradorPedido.getInstancia().AltaPedido(comandas, cantidades, idMesa, nroMozo,LocalId);

        	request.setAttribute("mensaje", "Todo Bien");
        	jspPage = "/jsp/ResultadoAbrirPedido.jsp";
        	 
        	
        }
        
        System.out.println(action);
        dispatch(jspPage, request, response);
	}
	
	
    protected void dispatch(String jsp, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        if (jsp != null)
        {
            RequestDispatcher rd = request.getRequestDispatcher(jsp);
            rd.forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doPost(request, response);
    }


}
